package com.hddz.comlib.packet;

import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.hddz.comlib.model.Sensor;
import com.hddz.comlib.util.ConvertUtility;

/**
 * 历史数据包
 * 
 * @author hddz
 * 
 */
public class HistorySamplePacket extends Packet {
	private final String TAG = "HistoryPacket";
	private SimpleDateFormat dateformat = new SimpleDateFormat("yyMMdd HHmmssSSS");
	/**
	 * 站号
	 */
	private int stationId;

	/**
	 * 拆分实时数据包
	 */
	private List<RealTimePacket> realTimePackets = new ArrayList<RealTimePacket>();
	/**
	 * 包括
	 */
	private Sensor vSensor;

	private int startIndex = -1;

	private int count;

	public int getStationId() {
		return stationId;
	}

	public void setStationId(int stationId) {
		this.stationId = stationId;
	}

	public List<RealTimePacket> getRealTimePackets() {
		return realTimePackets;
	}

	public int getStartIndex() {
		return startIndex;
	}

	public int getCount() {
		return count;
	}

	@Override
	public String createViewText() {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("站号:" + this.stationId);
		sb.append("\r\n");
		for (RealTimePacket r : this.realTimePackets) {
			sb.append("时间:" + dateformat.format(r.sendTime));
			sb.append(" 序号:" + r.getIndex());
			sb.append("\r\n");
			for (Sensor s : r.getSensors()) {

				sb.append("通道:" + s.getChannel());
				sb.append(" 值 :" + s.getValue());
				sb.append(" | ");
			}
			sb.append("\r\n");
		}
		return sb.toString();
	}

	@Override
	public Boolean parse(ByteBuffer data, int len) {
		// TODO Auto-generated method stub
		byte[] b = new byte[len];
		int perPosition = data.position();
		String time = "";
		int index = 0;
		try {
			data.get(b, 0, len);
			this.sourceText = ConvertUtility.bystesToStr(b, 0, len);
			Log.d(TAG, this.sourceText);
			data.position(perPosition);
			stationId = ((data.get() & 0xff) * 256 + (data.get() & 0xff)) * 256 + (data.get() & 0xff);

			count = data.get() & 0xff;

			vSensor = new Sensor();
			vSensor.setStationId(stationId);
			vSensor.setChannel("V");
			vSensor.setValue(((data.get() & 0xff) * 2) / 100.0);// 电池电压解数据时，先乘以2，再除以100
			// 原来的解法 (data[index] * 256 + data[index + 1]) /
			// 100.0;//电池电压解数据时，除以100；

			for (int i = 0; i < count; i++) {
				index = (data.get() & 0xff) * 256 + (data.get() & 0xff);
				if (startIndex < 0) {
					startIndex = index;
				}
				time = String.format("%02d", data.get() & 0xff) + 
						String.format("%02d", data.get() & 0xff)
						+ String.format("%02d", data.get() & 0xff) + " " 
						+ String.format("%02d", data.get() & 0xff)
						+ String.format("%02d", data.get() & 0xff) 
						+ String.format("%02d", data.get() & 0xff) 
						+ String.format("%03d", index % 1000);

				RealTimePacket r = new RealTimePacket();
				r.setSendTime(dateformat.parse(time));
				r.setSendTimes(time);
				r.setIndex(index);

				Sensor sensor = new Sensor();
				sensor.setStationId(stationId);
				sensor.setChannel("P3");
				sensor.setValue(data.get() & 0xff);
				r.getSensors().add(sensor);

				realTimePackets.add(r);
			}

			this.viewText = this.createViewText();
			Log.d(TAG, this.viewText);
			return true;
		} catch (Exception e) {
			// CLogger.Write("HistoryPacket Parse Error" + e.Message);
			return false;
		}
	}

	@Override
	public Boolean parse(List<Byte> data, int start, int len) {
		// TODO Auto-generated method stub
		return null;
	}
}